//
//  FKRefundExplainCell.m
//  WanTao
//
//  Created by Macbook Pro on 2019/4/3.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKRefundExplainCell.h"

@interface FKRefundExplainCell ()

@property (nonatomic, weak) UILabel *descLabel;
@property (nonatomic, strong) NSArray *picViews;

@end

@implementation FKRefundExplainCell

- (void)setupSubviews
{
    [super setupSubviews];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UILabel *titleLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333 text:@"退款说明"];
    [self.contentView addSubview:titleLabel];
    
    UIView *separator = [[UIView alloc] init];
    [self.contentView addSubview:separator];
    separator.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UILabel *descLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333];
    [self.contentView addSubview:descLabel];
    self.descLabel = descLabel;
    descLabel.numberOfLines = 0;
    
    NSMutableArray *picViews = [NSMutableArray array];
    for (NSInteger index = 0; index < 9; index++) {
        UIImageView *picView = [[UIImageView alloc] init];
        [self.contentView addSubview:picView];
        [picViews addObject:picView];
        picView.userInteractionEnabled = true;
        [picView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPicture:)]];
    }
    self.picViews = picViews;
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.top.offset(0);
        make.height.offset(fk_adjustW(45));
    }];
    [separator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(titleLabel.mas_bottom);
        make.height.offset(0.8);
    }];
    [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.offset(0);
        make.left.offset(12);
        make.right.offset(-12);
        make.top.equalTo(separator.mas_bottom).offset(fk_adjustW(12));
    }];
}

- (void)showPicture:(UITapGestureRecognizer *)gesture
{
    if ([self.delegate respondsToSelector:@selector(refundExplainCell:showPicture:index:)]) {
        NSArray *images = self.data[@"refund_images"];
        UIImageView *imageView = (UIImageView *)gesture.view;
        [self.delegate refundExplainCell:self showPicture:images index:[self.picViews indexOfObject:imageView]];
    }
}

- (void)setData:(id)data
{
    [super setData:data];
    
    if ([data isKindOfClass:[NSDictionary class]]) {
        self.descLabel.text = data[@"refund_content"];
        NSArray *imageFrames = data[@"imageFrames"];
        NSArray *images = data[@"refund_images"];
        for (NSInteger index = 0; index < self.picViews.count; index++) {
            UIImageView *picView = self.picViews[index];
            if (index < imageFrames.count) {
                [picView sd_setImageWithURL:[NSURL URLWithString:images[index]] placeholderImage:kPlaceholder_1x1];
                picView.frame = [imageFrames[index] CGRectValue];
            } else {
                picView.frame = CGRectZero;
            }
        }
    }
}

@end
